package com.pybox.spec;

import com.pybox.common.utils.DateUtils;
import com.pybox.entity.TCanbinetEventLogs;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
* 描述：TCanbinetEventLogs Specification
* @author wuyingkai
* @date 2018-09-15
*/


public class TCanbinetEventLogsSpec implements Specification<TCanbinetEventLogs> {

    private Map<String, String> params;

    public TCanbinetEventLogsSpec(Map<String, String> params) {
        this.params = params;
    }

    @Override
    public Predicate toPredicate(Root<TCanbinetEventLogs> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {


        String startTime = this.params.get("startTime");
        String endTime = this.params.get("endTime");
        String userId = this.params.get("userId");
        String customerId = this.params.get("customerId");
        String cabinetId = this.params.get("cabinetId");
        String objectId = this.params.get("objectId");
        String event = this.params.get("event");
        String level = this.params.get("level");
        String state = this.params.get("state");
        List<Predicate> ps = new ArrayList<>();


        if (!StringUtils.isEmpty(startTime) && StringUtils.isEmpty(endTime)) {//只选开始时间
            ps.add(criteriaBuilder.greaterThanOrEqualTo(root.get("ctime"), startTime));
        }
        if (StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {//只选结束时间
            ps.add(criteriaBuilder.lessThanOrEqualTo(root.get("ctime"), startTime));
        }
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)) {//开始和结束
            ps.add(criteriaBuilder.greaterThanOrEqualTo(root.get("ctime"), startTime));
            ps.add(criteriaBuilder.lessThanOrEqualTo(root.get("ctime"), startTime));
        }
        if (!StringUtils.isEmpty(userId)) {
            ps.add(criteriaBuilder.like(root.get("userId"), "%" + userId + "%"));
        }
        if (!StringUtils.isEmpty(customerId)) {
            ps.add(criteriaBuilder.equal(root.get("customerId"), customerId));
        }
        if (!StringUtils.isEmpty(cabinetId)) {
            ps.add(criteriaBuilder.like(root.get("cabinetId"), "%" + cabinetId + "%"));
        }
        if (!StringUtils.isEmpty(objectId)) {
            ps.add(criteriaBuilder.like(root.get("objectId"), "%" + objectId + "%"));
        }
        if (!StringUtils.isEmpty(event)) {
            ps.add(criteriaBuilder.equal(root.get("event"), event));
        }
        if (!StringUtils.isEmpty(level)) {
            ps.add(criteriaBuilder.equal(root.get("level"), level));
        }
        if (!StringUtils.isEmpty(state)) {
            ps.add(criteriaBuilder.equal(root.get("state"), state));

            criteriaQuery = criteriaQuery.where(ps.toArray(new Predicate[ps.size()]));

        }
        return criteriaQuery.getRestriction();
    }
}

